package org.shop.controller.upload;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.shop.util.FileUploadUtil;

/**
 * 文件上传 - 封装上传工具
 * 
 * @author zqx
 *
 */
@WebServlet("/admin/upload4.do")
@MultipartConfig
public class Upload4Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取非文件域的数据，仍然使用之前的getParameter和getParameterValues方法获取
		String filename = req.getParameter("filename") ;
		Part part = req.getPart("attach") ;
		
		// 2.实例化文件上传工具对象
		FileUploadUtil fileUploadUtil = new FileUploadUtil(2*1024*1024,"ppt,pptx,txt");
		String path = this.getServletContext().getRealPath("/file") ;
		
		// 3.上传文件
		String name = fileUploadUtil.upload(part, path) ;
		
		// 返回的文件名，需要存储在数据库中
		System.out.println("普通控件的值：" + filename);
		System.out.println("上传文件的名称：" + name);
		
	}
}





